# -*- coding: utf-8 -*-
# created on 2016/12/06

from mathsolver.functions.base import *
from sympy.abc import n
from sympy import expand
from mathsolver.functions.shulie.ShuLie_getSeqPoly import ShuLieGetSeqPoly

def InfMuls_aNames(expr):
    aNames = []
    for i, item in enumerate(expr.split('*')):
        try:
            val = sympify(item, evaluate=False)
            aName = val.func
        except:
            pass
        aNames.append(aName)
    aNames = set(aNames)
    aNames = list(aNames)
    return aNames

def InfMuls_items(expr):
    items = []
    for i, item in enumerate(expr.split('*')):
        try:
            val = sympify(item, evaluate=False)  # evluate=False => 分子分母不自动化简
        except:
            val = None
        items.append(val)
    return items

def InfMuls_indexs(items):
    indexs = []
    for item in items:
        try:
            index = item.args[0]
        except:
            index = None
        indexs.append(index)
    return indexs

def InfMuls_TongXiang(indexs):
    TongXiang = None
    d = indexs[1] - indexs[0]
    if indexs[2]:
        d2 = indexs[2] - indexs[1]
        if d == d2:
            TongXiang = indexs[0] + (n - 1) * d
    else:
        TongXiang = indexs[0] + (n - 1) * d
    return TongXiang

# 设等比数列{a_{n}}满足a_{2}+a_{3}=10,a_{2}+a_{4}=5,则a_{2}a_{2}…a_{n}的最大值为().
class ShuLie_getInfMultiply001(BaseFunction):
    def solver(self, *args):
        assert len(args) == 1
        assert isinstance(args[0], baseSeqinfpoly)
        poly = args[0].value
        poly_names = InfMuls_aNames(poly)
        assert len(poly_names) == 1
        poly_name = poly_names[0]
        poly_items = InfMuls_items(poly)
        poly_indexs = InfMuls_indexs(poly_items)
        poly_index_tongxiang = InfMuls_TongXiang(poly_indexs)
        ShuLie = self.search(str(poly_name))
        ShuLieType = ShuLie.type
        symbol = ShuLie.nName
        assert ShuLieType == "DengBi"
        known = self.known
        stepsolver = sl_getSeqPoly(known).solver(baseSeqpoly(poly_name(symbol)))
        self.steps += stepsolver.steps
        self.label.update(stepsolver.label)
        TongXiangs = stepsolver.output[0].toSympy()
        assert len(TongXiangs) == 1
        TongXiang = TongXiangs[0]
        TongXiang_base = TongXiang.args[0]
        TongXiang_exp = TongXiang.args[1]
        assert sl_LinearN(poly_index_tongxiang, symbol)
        a, b = sl_LinearPolyCoeffs(poly_index_tongxiang, symbol)
        num = (poly_indexs[-1] - poly_indexs[0]) / a + 1
        m, n = sl_LinearPolyCoeffs(TongXiang_exp, symbol)
        exp_sums = num * TongXiang_exp.subs({symbol: poly_indexs[0]}) + num * (num - 1) * m / 2
        exp_sums = expand(exp_sums)
        exp_sums = simplify(exp_sums)
        nMuls = TongXiang_base ** exp_sums
        self.steps.append(["", "∴ %s = %s" %(poly, latex(nMuls))])
        self.output.append(baseNumbers([nMuls]))
        return self

class ShuLie_getInfMultiply(BaseFunction):
    CLS = [ShuLie_getInfMultiply001]
    def solver(self, *args):
        known = self.known
        r = None
        for cl in ShuLie_getInfMultiply.CLS:
            try:
                new_known = dict(known)
                r = cl(new_known, verbose=True).solver(*args)
                break
            except Exception:
                pass
        if not r:
            raise 'try fail'
        return r

if __name__ == '__main__':
    names = InfMuls_aNames('a_(2)*a_(2)*...*a_(n)')
    items = InfMuls_items('a_(2)*a_(2)*...*a_(n)')
    indexs = InfMuls_indexs(items)
    # print names
    # print items
    # print indexs